package com.android.zweibo.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.android.zweibo.bean.Common;
import com.android.zweibo.bean.Task;
import com.android.zweibo.logic.MainService;
import com.android.zweibo.util.JavaScriptInterface;

public class WebViewActivity extends Activity implements IWeiboActivity{
	private ProgressDialog proDialog ;
	private WebView webView;
	private String url = null;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			if(msg.what == Common.DIALOG_CLOSE ) {
				proDialog.dismiss();
			}
		};
	};
	
   @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);

		//获取授权url
		Task task = new Task(Common.AUTHORIZE,null);
		MainService.newTask(task);
		MainService.addActivity(this);
		
	
		System.out.println("url----"+ url);
		init();
		

	}
   
   /**
    * 初始化
    */
   public  void init() {
	    if(null == proDialog) {
	    	proDialog = new ProgressDialog(this);
	    }
	    proDialog.setMessage("正在加载");
	    proDialog.show();
	    webView = (WebView) this.findViewById(R.id.weiView);
	   	
	   	webView.getSettings().setJavaScriptEnabled(true);
	   	
	   	//添加新的javaScript解析
	   	webView.addJavascriptInterface(new JavaScriptInterface(),"Methods");
	   	
	   	webView.setWebViewClient(new WebViewClient(){
	   		@Override
	   		public boolean shouldOverrideUrlLoading(WebView view, String url) {
	   			load(url,webView);
	   			return true;
	   		}
	   		
	   		
	   		@Override
	   		public void onPageFinished(WebView view, String url) {
                System.out.println("url----url:" + url);
                
	   			if(url.equals("http://api.t.sina.com.cn/oauth/authorize")) {

	   				view.loadUrl("javascript:window.Methods.getPin('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
	                Intent intent = new Intent(WebViewActivity.this,AccessTokenActivity.class);
	                startActivity(intent);
	   			}
	   			super.onPageFinished(view, url);
	   		}
	   	});
	   	
	   	webView.setWebChromeClient(new WebChromeClient() {
	   		@Override
	   		public void onProgressChanged(WebView view, int newProgress) {
	   			
	   			if(newProgress == 100) {
	   				handler.sendEmptyMessage(Common.DIALOG_CLOSE);
	   			}
	   			super.onProgressChanged(view, newProgress);
	   		}
	   	});
	   	
	   	
   }
   
   /**
    * 加载页面
    * @param url
    * @param webView
    */
   public void load(final String url, final WebView webView) {
	   	if(null != url || "".equals(url)) {
	   		new Thread() {
	   			@Override
	   			public void run() {
	   				webView.loadUrl(url);
	   			}
	   		}.start();
	   	}
   }
  
   @Override
protected void onPause() {
	// TODO Auto-generated method stub
	super.onPause();
	this.finish();
}

@Override
public void refresh(Object... objects) {
	proDialog.setMessage("正在获取url");
	url = (String) objects[0];
	load(url,webView);
}
  
}

